package com.yzc.android.ui.base.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.LayoutRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.FrameLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import com.yzc.android.R;
import com.yzc.android.ui.base.ActivityCollector;
import com.yzc.android.ui.base.IKDActivity;
import com.yzc.android.ui.base.lifecycle.IComponentContainer;
import com.yzc.android.ui.base.lifecycle.ILifeCycleComponent;
import com.yzc.android.ui.base.lifecycle.LifeCycleComponentManager;
import com.yzc.android.widget.RentalsSunHeaderView;
import com.yzc.android.widget.TitleHeaderBar;
import in.srain.cube.views.ptr.PtrClassicDefaultHeader;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.header.MaterialHeader;
import in.srain.cube.views.ptr.header.StoreHouseHeader;

/**
 * Created by yzc on 16/4/7.
 */
public abstract class KDBaseActivity extends Activity implements IComponentContainer, IKDActivity {

    protected boolean mIsForeground = false;

    protected @Bind(R.id.navigation_bar)
    TitleHeaderBar mNavigationBar;

    protected PtrFrameLayout mPtrFrameLayout;
    protected FrameLayout mContainerLayout;
    protected PtrUIHandler mDefaultHeader;

    protected Handler mHandler = new Handler();

    private LifeCycleComponentManager mComponentContainer = new LifeCycleComponentManager();

    // 下拉刷新
    protected abstract @LayoutRes int getLayout();

    // 页面构造完之后调用子类的页面加载
    protected abstract void setupView(final Bundle savedInstanceState);

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCollector.addActivity(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_activity_base_navbar);
        mContainerLayout = (FrameLayout) findViewById(R.id.main_view_container);
        mPtrFrameLayout = (PtrFrameLayout) findViewById(R.id.main_view_ptr_frame_layout);
        View contentView = LayoutInflater.from(this).inflate(getLayout(), null);
        mContainerLayout.addView(contentView);
        ButterKnife.bind(this);
        getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {
                if (mNavigationBar != null) {
                    if (enableDefaultBack()) {
                        mNavigationBar.setLeftOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                onBackPressed();
                            }
                        });
                    } else {
                        mNavigationBar.getLeftViewContainer().setVisibility(View.INVISIBLE);
                    }
                }

                if (enablePullRefresh()) {
                    // 下拉刷新
                    loadDefaultRefreshHeader();
                    //loadStoreHouseRefreshHeader();
                    //loadMaterialRefreshHeader();
                    //loadRentalRefreshHeader();
                }

                setupView(savedInstanceState);
            }
        });
        //EventBusManager.bindContainerAndHandler(this, new EventLifeCycleHandler()
        //{
        //    public void onEvent(NetworkStateEvent event) {
        //        LogUtils.d(NetworkStatusManager.getInstance().getNetworkTypeName());
        //    }
        //}).tryToRegisterIfNot();
    }

    private void loadDefaultRefreshHeader()
    {
        PtrClassicDefaultHeader defaultHeader = new PtrClassicDefaultHeader(this);
        mPtrFrameLayout.removePtrUIHandler(mDefaultHeader);
        mPtrFrameLayout.setHeaderView(defaultHeader);
        mPtrFrameLayout.addPtrUIHandler(defaultHeader);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(final PtrFrameLayout frame, final View content, final View header) {
                boolean canDoPullDown = PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
                return canDoPullDown && enablePullRefresh();
            }

            @Override
            public void onRefreshBegin(final PtrFrameLayout frame) {
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPtrFrameLayout.refreshComplete();
                        pullToRefreshHandler();
                    }
                }, 1800);
            }
        });
        mDefaultHeader = defaultHeader;
    }

    private void loadStoreHouseRefreshHeader()
    {
        StoreHouseHeader houseHeader = new StoreHouseHeader(this);

        //houseHeader.initWithString("XCode Big Air");
        houseHeader.initWithStringArray(R.array.akta);
        mPtrFrameLayout.removePtrUIHandler(mDefaultHeader);
        mPtrFrameLayout.setHeaderView(houseHeader);
        mPtrFrameLayout.addPtrUIHandler(houseHeader);
        mPtrFrameLayout.autoRefresh(false);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(final PtrFrameLayout frame, final View content, final View header) {
                boolean canDoPullDown = PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
                return canDoPullDown && enablePullRefresh();
            }

            @Override
            public void onRefreshBegin(final PtrFrameLayout frame) {
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPtrFrameLayout.refreshComplete();
                        pullToRefreshHandler();
                    }
                }, 1800);
            }
        });
        mDefaultHeader = houseHeader;
    }

    private void loadMaterialRefreshHeader()
    {
        MaterialHeader materialHeader = new MaterialHeader(this);
        int[] colors = getResources().getIntArray(R.array.google_colors);
        materialHeader.setColorSchemeColors(colors);
        materialHeader.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
        materialHeader.setPadding(0, 15, 0, 10);
        materialHeader.setPtrFrameLayout(mPtrFrameLayout);
        mPtrFrameLayout.removePtrUIHandler(mDefaultHeader);
        mPtrFrameLayout.setHeaderView(materialHeader);
        mPtrFrameLayout.addPtrUIHandler(materialHeader);
        mPtrFrameLayout.autoRefresh(false);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(final PtrFrameLayout frame, final View content, final View header) {
                boolean canDoPullDown = PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
                return canDoPullDown && enablePullRefresh();
            }

            @Override
            public void onRefreshBegin(final PtrFrameLayout frame) {
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPtrFrameLayout.refreshComplete();
                        pullToRefreshHandler();
                    }
                }, 1800);
            }
        });
        mDefaultHeader = materialHeader;
    }

    private void loadRentalRefreshHeader() {
        RentalsSunHeaderView rentalsSunHeaderView = new RentalsSunHeaderView(this);
        rentalsSunHeaderView.setUp(mPtrFrameLayout);
        mPtrFrameLayout.removePtrUIHandler(mDefaultHeader);
        mPtrFrameLayout.setHeaderView(rentalsSunHeaderView);
        mPtrFrameLayout.addPtrUIHandler(rentalsSunHeaderView);
        mPtrFrameLayout.autoRefresh(false);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(final PtrFrameLayout frame, final View content, final View header) {
                boolean canDoPullDown = PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
                return canDoPullDown && enablePullRefresh();
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                ptrFrameLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPtrFrameLayout.refreshComplete();
                        pullToRefreshHandler();
                    }
                }, 1800);
            }
        });
        mDefaultHeader = rentalsSunHeaderView;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mComponentContainer.onDestroy();
        ActivityCollector.removeActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mComponentContainer.onBecomesVisibleFromPartiallyInvisible();
        mIsForeground = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        mComponentContainer.onBecomesPartiallyInvisible();
        mIsForeground = false;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mComponentContainer.onBecomesVisibleFromTotallyInvisible();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mComponentContainer.onBecomesTotallyInvisible();
    }

    @Override
    public void addComponent(final ILifeCycleComponent component) {
        mComponentContainer.addComponent(component);
    }

    @Override
    public boolean enableDefaultBack() {
        return true;
    }

    @Override
    public boolean enablePullRefresh() {
        return false;
    }

    @Override
    public void pullToRefreshHandler() {

    }

    protected void setHeaderTitle(int id) {
        mNavigationBar.getTitleTextView().setText(id);
    }

    protected void setHeaderTitle(String title) {
        mNavigationBar.getTitleTextView().setText(title);
    }
}
